/**
 * Created by dean on 2017/8/4.
 */
var http = require('http');
var fs = require('fs');

function serveStaticFile(res, path, contentType, responseCode) {
    if(!responseCode) responseCode = 200;

    fs.readFile(__dirname + path, function(err,data) {
        if(err) {
            res.writeHead(500, { 'Content-Type': 'text/plain' });
            res.end('500 - Internal Error');
        } else {
            res.writeHead(responseCode, { 'Content-Type': contentType });
            res.end(data);
        }
    });
}

http.createServer(function(req,res){
    var path = req.url.replace(/\/?(?:\?.*)?$/, '').toLowerCase();
    switch(path) {
        case '':
            serveStaticFile(res,'/public/GoogleMap.html','text/html');
            break;
        case '/about':
            res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('About');
            break;
        default:
            res.writeHead(404, { 'Content-Type': 'text/plain' }); res.end(path);
            break;
    }
}).listen(3000);

console.log('Server started on localhost:3000l;press ctrl-C  to terminate....');